//
//  Car.m
//  FunnyRacing
//
//  Created by vy phan on 15/03/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Car.h"
#import "Game.h"
#import "GraphicConstant.h"

#define PTM_RATIO 32

@implementation Car

@synthesize speed;
@synthesize solid;
@synthesize accel;
@synthesize carSheet;
@synthesize rotationStep;
@synthesize backSpeed;
@synthesize maxSpeed;
@synthesize speedDecay;
@synthesize code;

- (Car*)initCarWithType:(int)type_
{
	Car* car = nil;
	switch (type_) {
		case WCAR:
			carSheet = [CCSpriteSheet spriteSheetWithFile:@"whitecar1.png" capacity:1];
			car = [[Car alloc] initWithTexture:carSheet.texture rect:CGRectMake(0, 0, CAR_WIDTH, CAR_HEIGHT)];
			car.speed = 0;
			car.solid = 5;
			car.accel = 1;
			car.speedDecay = 0.003;
			car.maxSpeed = 3;
			car.backSpeed = 2;
			car.code = @"white";
			car.rotationStep = 2;
			car.rotation = 90;
			[self animatedCar:car];
			
			break;
		default:
			break;
	}
	return car;
}
- (void)animatedCar:(Car*)car_
{
	CCAnimation *animation = [CCAnimation animationWithName:@"carAni" delay:0.1];
	for (int i = 0; i < CAR_FRAME; i++) {
		[animation addFrameWithTexture:carSheet.texture rect:CGRectMake(i * CAR_WIDTH, 0, CAR_WIDTH, CAR_HEIGHT)];
	}
	[car_ runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
}
//- (void)addBoxBodyForSprite:(CCSprite *)sprite {
//    
//    b2BodyDef spriteBodyDef;
//    spriteBodyDef.type = b2_dynamicBody;
//    spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
//    spriteBodyDef.userData = sprite;
//    b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);
//	
//    b2PolygonShape spriteShape;
//    /*spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,
//	 sprite.contentSize.height/PTM_RATIO/2);*/
//    int num = 6;
//	b2Vec2 verts[] = {b2Vec2(-15.0f / PTM_RATIO, 30.0f / PTM_RATIO),
//		b2Vec2(-16.0f / PTM_RATIO, 29.0f / PTM_RATIO),
//		b2Vec2(15.0f / PTM_RATIO, 29.0f / PTM_RATIO),
//		b2Vec2(14.0f / PTM_RATIO, -29.0f / PTM_RATIO),
//		b2Vec2(-17.0f / PTM_RATIO, -27.0f / PTM_RATIO),
//		b2Vec2(-16.0f / PTM_RATIO, 30.0f / PTM_RATIO)};
//    
//    b2FixtureDef spriteShapeDef;
//    spriteShapeDef.shape = &spriteShape;
//    spriteShapeDef.density = 10.0;
//    spriteShapeDef.isSensor = true;
//	
//    spriteBody->CreateFixture(&spriteShapeDef);
//    
//}
@end
